﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

public partial class personaInfo : System.Web.UI.Page
{
    VLGDataContext _db = new VLGDataContext();
    CommonPage cm3 = new CommonPage();
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Title = "Trang cá nhân | Thông tin cá nhân";
        if (!IsPostBack)
        {
            try
            {
                if (cm3.IsAuthenticated == true)
                {
                    fillInfo();

                }
                else
                {
                    Response.Redirect(ResolveClientUrl("~/warnLogin.aspx"));
                }

            }
            catch {
                Response.Redirect(ResolveClientUrl("~/warnLogin.aspx"));
            }
        }
    }
    void fillInfo()
    {
        txt_email.Text = cm3.Customer.Email;
        txt_fullName.Text = cm3.Customer.fullName;
        txt_intro.Text = cm3.Customer.introYour;
        
        if (!String.IsNullOrEmpty(cm3.Customer.urlAvatar))
        {
            avatar_in.Src = "avatar/" + cm3.Customer.urlAvatar;
            avatar_in.Width = 100;
            avatar_in.Height = 100;
        }
        else
        {
            avatar_in.Src = "avatar/noavatar.jpg";
            avatar_in.Width = 100;
            avatar_in.Height = 100;
        }
    }
    protected void btnLuuInfo_Click(object sender, EventArgs e)
    {
        try
        {
            string user = cm3.Customer.UsernameE;
            string tiento = cm3.Customer.idAcc + cm3.Customer.UsernameE;
            string fullname = txt_fullName.Text.Trim().ToString();
            string email = txt_email.Text.Trim().ToString();
            string gioithieu = txt_intro.Text.Trim().ToString();
            if (FileUpload1.HasFile)
            {
                string[] filename = Path.GetFileName(FileUpload1.FileName).Split('.');
                string tenfile = AllCode.Utils.ConvertToUnSign(filename[0].ToString());
                string duoifile=filename[1];
                string tenFileHoanChinh=tiento+"_"+tenfile+"."+duoifile;
                if (!duoifile.Equals("jpg"))
                {
                    litFil.Text = "<script type='text/javascript'>alert('Chỉ chấp nhận file Jpg')</script>";
                    return;
                }else if (FileUpload1.PostedFile.ContentLength > 1048576)
                {
                    litFil.Text = "<script type='text/javascript'>alert('Ảnh không được vượt quá 1 MB')</script>";
                    return;
                    //Response.Write(FileUpload1.FileBytes.ToString());
                }
                else
                {
                    VLGAccount accUpdate = _db.VLGAccounts.Single(p=>p.UsernameE==user);
                    accUpdate.urlAvatar = tenFileHoanChinh;
                    accUpdate.fullName = fullname;
                    accUpdate.Email = email;
                    accUpdate.introYour = gioithieu;

                    _db.SubmitChanges();

                    cm3.Customer.urlAvatar = tenFileHoanChinh;
                    cm3.Customer.Email = email;
                    cm3.Customer.introYour = gioithieu;
                    cm3.Customer.fullName = fullname;
                    avatar_in.Src = "avatar/"+tenFileHoanChinh;
                    thunhoVaLuu(100, 100, FileUpload1, tenFileHoanChinh);
                    litFil.Text = "<script type='text/javascript'>alert('Đăng ảnh thành công')</script>";
                   // Response.Redirect(Request.Url.AbsoluteUri);
                    fillInfo();
                    //updatePanelInfo.Update();
                }
            }
            else
            {
                VLGAccount accUpdate = _db.VLGAccounts.Single(p => p.UsernameE == user);
                accUpdate.fullName = fullname;
                accUpdate.Email = email;
                accUpdate.introYour = gioithieu;

                _db.SubmitChanges();
                
                cm3.Customer.Email = email;
                cm3.Customer.introYour = gioithieu;
                cm3.Customer.fullName = fullname;
                fillInfo();
            }
        }
        catch
        {
            Response.Redirect(ResolveClientUrl("~/warnLogin.aspx"));
        }
    }
    public bool thunhoVaLuu(int chieuRong, int chieuCao, FileUpload fulPhoto2, string tenfileThum)
    {
        bool flag = false;
        string pathAvatars = Server.MapPath(("avatar/"));
        try
        {
            int thumbWidth = chieuRong;
            int thumbHeight = chieuCao;

            System.Drawing.Image oImg = System.Drawing.Image.FromStream(fulPhoto2.PostedFile.InputStream);
            System.Drawing.Image oThumbNail = new Bitmap(thumbWidth, thumbHeight, oImg.PixelFormat);

            Graphics oGraphic = Graphics.FromImage(oThumbNail);
            oGraphic.CompositingQuality = CompositingQuality.HighQuality;
            oGraphic.SmoothingMode = SmoothingMode.HighQuality;
            oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;

            Rectangle oRectangle = new Rectangle(0, 0, thumbWidth, thumbHeight);
            oGraphic.DrawImage(oImg, oRectangle);

            oThumbNail.Save(pathAvatars + tenfileThum, ImageFormat.Jpeg);
            oImg.Dispose();
            flag = true;
            return flag;
        }
        catch (Exception ex)
        {
            return flag;
        }
    }
    protected void btn_change_Click(object sender, EventArgs e)
    {
        string mkhientai = txt_curPs.Text.ToString();
        string mkMoi = txt_mkMoi.Text.ToString();
        string mkMoiXacNhan = txt_xacnhan.Text.ToString();
        try
        {
            string userName= cm3.Customer.UsernameE;
            try
            {
                VLGAccount vlgacc = _db.VLGAccounts.Single(aa => aa.UsernameE == userName);
                if (vlgacc.Password != AllCode.Utils.Mahoa(mkhientai))
                {
                    Label1.Text = "Mật khẩu hiện tại Sai ";
                    reset3Textbox();
                    return;
                }
                else
                {
                    if (mkMoi != mkMoiXacNhan)
                    {
                        Label1.Text = "Mật khẩu xác nhận sai";
                        reset3Textbox();
                        return;
                    }
                    else
                    {
                        vlgacc.Password = AllCode.Utils.Mahoa(mkMoi);
                    }
                }
                _db.SubmitChanges();
                litFil.Text = "<script>alert('Cập nhật mật khẩu thành công !')</script>";
                reset3Textbox();
                
            }
            catch
            {
            }
        }
        catch
        {
            Response.Redirect(ResolveClientUrl("~/warnLogin.aspx"));
        }
    }
    void reset3Textbox()
    {
        txt_curPs.Text = "";
        txt_mkMoi.Text = "";
        txt_xacnhan.Text = "";
    }
}